package com.spzx.test1.Day3Test;
/*
    并非全力
*/

import org.apache.commons.lang3.RandomUtils;

import java.util.concurrent.*;

import static java.lang.Thread.sleep;

public class t3 {

    public static void main(String[] args) {

        ExecutorService service = Executors.newFixedThreadPool(2);
        CyclicBarrier barrier = new CyclicBarrier(2,()->{
            System.out.println("所有线程都到达栅栏，继续执行");
        });

        for (int i = 0; i < 3; i++) {

            service.submit(()->{
                System.out.println("task1 begin");
                try {
                    TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1,5));
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            });


            service.submit(()->{
                System.out.println("task2 begin");
                try {
                    TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1,5));
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        service.shutdown();

    }

}
